package com.app.module.camera

import android.graphics.SurfaceTexture
import android.view.Surface
import com.app.module.camera.core.DynamicProcessor
import com.app.module.camera.core.observer.FramePreviewer
import com.app.module.camera.core.observer.ImageCapturer
import com.app.module.camera.core.observer.VideoCapturer
import com.app.module.camera.core.provider.CameraProvider
import com.app.module.camera.renderer.GLRenderer
import com.app.module.camera.ui.Camera2Activity


class CameraMapper {

    private val shower : FramePreviewer
    private val imageCapture : ImageCapturer
    private val videoCapture : VideoCapturer

    private val process : DynamicProcessor
    private val camera : CameraProvider

    constructor(activity: Camera2Activity) {
        this.shower = FramePreviewer(activity)
        this.imageCapture = ImageCapturer(activity)
        this.videoCapture = VideoCapturer(activity)

        this.camera= CameraProvider(activity = activity, initPhotoMode = true)

        this.process = DynamicProcessor(camera)

        this.process.addObserver(shower)
        this.process.addObserver(imageCapture)
        this.process.addObserver(videoCapture)
        
    }

    fun setCameraChange(surface: SurfaceTexture, witch:Int, height:Int){
        camera.setOutputSize(witch , height)
    }
    fun setPreviewChange(surface: SurfaceTexture, witch:Int, height:Int){
        imageCapture.setOutputSize(witch, height)
        videoCapture.setOutputSize(witch, height)
        shower.setOutputSize(witch, height)
        shower.setSurface(Surface(surface))
    }

    fun setRenderer(renderer: GLRenderer){
        process.setRenderer(renderer)
    }

    fun open() {
        shower.open()
        process.start()
    }
    fun close() {
        shower.close()
        process.stop()
    }

    fun switchCamera(){
        process.switchCamera()
    }

    fun captureImage(){
        imageCapture.open()
    }

    fun captureVideo(){
        videoCapture.open(camera.getSensorOrientation())
    }

    fun isVideoRecording() = videoCapture.isOpen()
    
    fun isScreenShowing() = shower.isOpen()

    fun capturevideoOver(){
        videoCapture.close()
        //videoCapture.recycle()
    }

    fun rotateCamera(){
        camera.rotateCamera() ;
    }
}
